Ti.include('../viewHelper/settingsViews.js');
Ti.include('../class/db.js');

var win = Ti.UI.currentWindow;
win.layout = 'vertical';
var views = settingsViews.getViews();

var activityIndicatorView = Ti.UI.createView({
	left:0,
	right:0,
	top:0,
	bottom:0,
	backgroundColor:'#000',
	opacity:0.8,
	visible:false
});

var activityIndicator = Ti.UI.createActivityIndicator({
	top:160,
	height:60,
	width:'auto',
	color:'#FFFFFF',
	style:Ti.UI.iPhone.ActivityIndicatorStyle.BIG,
	message:L("Comprovant usuari"),
	visible:true
});
activityIndicatorView.add(activityIndicator);


//HEADER
var header = Ti.UI.createView({height:40});
var headerLabel = Ti.UI.createLabel({
	top:10,
	left:20,
	textAlign:'left', 
	height:'auto',
	text:L("Ideatorrent"),
	color:'#FFF',
	font:{fontSize:16}
});
header.add(headerLabel);
win.add(header);

//BODY
var body = Ti.UI.createView({
	height:'auto', 
	left:10,
	right:10,
	layout:'vertical',
	backgroundColor:'#444',
	//borderColor:'#CCC',
	borderRadius:12
	//borderWidth:2
});


var userLabel = Ti.UI.createLabel({ 
	height:20,
	text:L('Usuari'),
	color:'#FFF',
	left:20, right:20,top:5,
	font:{fontSize:15}
});

var userText = Titanium.UI.createTextField({
	height:40,
	//width:'auto',
	hintText: L("Introduir Usuari"),
	backgroundColor:'#CCC',
	borderColor:'#444',
	paddingLeft:10,
	borderRadius:12,
	borderWidth:2,
	//borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED,
	color:'#000',
	font:{fontSize:14},
	left:15, right:15, top:5,
	clearButtonMode:Ti.UI.INPUT_BUTTONMODE_ONFOCUS,
	suppressReturn:true,
	autocorrect:false,
	autocapitalization: Ti.UI.TEXT_AUTOCAPITALIZATION_NONE,
	value:Ti.App.user.getLogin()
});

var passwordLabel = Ti.UI.createLabel({ 
	height:20,
	text:L('Password'),
	color:'#FFF',
	font:{fontSize:15},
	left:20, right:20, top:10
});

var passwordText = Titanium.UI.createTextField({
	height:40,
	//width:'auto',
	hintText: L("Introduir Password"),
	backgroundColor:'#CCC',
	borderColor:'#444',
	paddingLeft:10,
	borderRadius:12,
	borderWidth:2,
	color:'#000',
	font:{fontSize:14},
	left:15, right:15, top:5, bottom:15,	
	clearButtonMode:Ti.UI.INPUT_BUTTONMODE_ONFOCUS,
	suppressReturn:true,
	autocapitalization: Ti.UI.TEXT_AUTOCAPITALIZATION_NONE,
	passwordMask:true,
	autocorrect:false,
	value:Ti.App.user.getPassword()
});

body.add(userLabel);
body.add(userText);

body.add(passwordLabel);
body.add(passwordText);

//FOOTER
var footer = Ti.UI.createView({height:50,layout:'horizontal'});
var saveButton = Ti.UI.createButton({
	font:{size:"8"},
	left:30,
	bottom:10,
	height:40,
	width:100,
	title:L('Guardar')
	//textAlign:'left'
});

var deleteButton = Ti.UI.createButton({
	font:{size:"8"},
	bottom:10,
	left:40,
	height:40,
	width:100,
	title:L('Borrar')
	//textAlign:'left'
});

footer.add(saveButton);
footer.add(deleteButton);
body.add(footer);

win.add(body);
win.add(activityIndicatorView);

saveButton.addEventListener('click', function(){
	var user = userText.value;
	var password = passwordText.value;
	if(user!= null && user != "" && password != null && password != "" ){
		userText.blur();
		passwordText.blur();
		Ti.App.user.save(user,password);
		activityIndicatorView.show();
		Ti.App.network_user.getTokenFromWeb(user,password);
	}
});

deleteButton.addEventListener('click', function(){
	userText.value = '';
	passwordText.value = '';
	userText.blur();
	passwordText.blur();
	Ti.App.user.clear();
});

passwordText.addEventListener('return', function(){
	saveButton.fireEvent('click');
});


Ti.App.addEventListener('tokenDownloadOk', function(tokenObj){
	activityIndicatorView.hide();
	Ti.API.info("el token es  " + tokenObj.token);
	Ti.App.user.setToken(tokenObj.token);
	Ti.App.network_user.showAlert('',L("Usuari valid"));
});

Ti.App.addEventListener('tokenDownloadKo', function(tokenObj){
	activityIndicatorView.hide();
	Ti.API.info("ERROR " + tokenObj.error);
	var message = null;
	if (tokenObj.error == 'invalid user'){
		message = L('Usuari o contrasenya invalids');
	}else if(tokenObj.error == 'unknown error'){
		message = L('Error desconegut');
	}else{
		message = L('Your device is not online.');
	}
	Ti.App.network_user.showAlert(L('WARNING'),message);
});